﻿using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Xml;
using LitJson;
using MediaPortal.GUI.Library;
using System.Threading;
using System.Net;
using System.ComponentModel;
using System.IO;

namespace MediaPortal.Plugins.Trailers.Providers
{
    public class MovieMazeTrailerOverview
    {
        private const string urlOverview = MovieMazeTrailers.urlBase + "/media/trailer/index.phtml";
        private const string urlOverviewHD = MovieMazeTrailers.urlBase + "/media/trailer/index.phtml?update=hd";

        private const string tableRegExp = "<table.*?>(?<tablecontent>.*?)<\\/table>";
        private const string trailerListRegExp = "(?<trailerurl>\\/media\\/trailer\\/(?!index).*?.html)\">(?<trailername>.*?)<\\/a>";
        private const string trailerListRegExpDetail = trailerListRegExp + "\\s*?<span.*?>(?<detail>.*?)<\\/span>";

        public Index fetchLastUpdates()
        {
            return fetch(urlOverview, "Letzte Updates", trailerListRegExpDetail);
        }

        public Index fetchTop10()
        {
            return fetch(urlOverview, "Top 10", trailerListRegExpDetail, true);
        }

        public Index fetchRecent()
        {
            return fetch(urlOverview, "Aktuelle Trailer", trailerListRegExp);
        }

        public Index fetchLastUpdatesHD()
        {
            return fetch(urlOverviewHD, "Letzte Updates", trailerListRegExpDetail);
        }

        private Index fetch(string url, string identifier, string regExp)
        {
            return fetch(url, identifier, regExp, false);
        }

        private Index fetch(string url, string identifier, string regExp, bool addNumbers)
        {
            Index items = new Index();
            string html = string.Empty;

            MovieMazeTrailerWebClient client = new MovieMazeTrailerWebClient();
            client.GetWebPage(url, MovieMazeTrailers.urlBase, out html);

            MatchCollection mc = Regex.Matches(html, @tableRegExp, RegexOptions.Singleline);

            

            foreach (Match m in mc)
            {
                string region = m.Groups["tablecontent"].Value;
                if (region.Contains(@identifier))
                {
                    int number = 1; 
                    MatchCollection mco = Regex.Matches(region, @regExp);

                    foreach (Match ma in mco)
                    {
                        IndexItem item = new IndexItem();
                        item.Label = ma.Groups["trailername"].Value;

                        string detail = ma.Groups["detail"].Value;
                        if (!string.IsNullOrEmpty(detail))
                        {
                            item.Label += " - " + detail.Trim();
                        }

                        if (addNumbers)
                        {
                            item.Label = number + ") " + item.Label;
                        }

                        item.Key = ma.Groups["trailerurl"].Value;
                        items.Add(item);

                        number++;
                    }
                }

               
            }

            return items;
        }
    }
}